# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
_luaversions="5.1 5.2 5.3 5.4"
pkgname=lua-openrc
pkgver=0.2
pkgrel=6
pkgdesc="Lua module for OpenRC"
url="https://github.com/ncopa/lua-openrc"
arch="all"
license="GPL-2.0-only"
makedepends="openrc-dev bsd-compat-headers"
for _i in $_luaversions; do
	makedepends="$makedepends lua$_i-dev"
	subpackages="$subpackages lua$_i-openrc:_split:$CARCH"
done
source="lua-openrc-$pkgver.tar.gz::https://github.com/ncopa/lua-openrc/archive/v$pkgver.tar.gz
	0001-build-fix-for-openrc-0.14.patch
	"
options="!check"

prepare() {
	default_prepare
	for _i in $_luaversions; do
		cp -r "$builddir" "$srcdir"/build-$_i
	done
}

build() {
	for _i in $_luaversions; do
		cd "$srcdir"/build-$_i
		make LUA_VERSION=$_i
	done
}

package() {
	for _i in $_luaversions; do
		install -Dm755 "$srcdir"/build-$_i/rc.so \
			"$pkgdir"/usr/lib/lua/$_i/rc.so
	done
}

_split() {
	local d= _ver=${subpkgname%%-*}
	_ver=${_ver#lua}
	pkgdesc="Lua $_ver module for OpenRC"
	install_if="lua$_ver $pkgname=$pkgver-r$pkgrel"
	depends=
	for d in usr/lib/lua usr/share/lua; do
		if [ -d "$pkgdir"/$d/$_ver ]; then
			mkdir -p "$subpkgdir"/$d
			mv "$pkgdir"/$d/$_ver "$subpkgdir"/$d/
		fi
	done
}

sha512sums="f560cbef63d7d1565dacd9ec2f8dc91d4c08397dfcd546dd446d3834caef8d545cab6d0b7ea5a095090e3069935eb9adfa35e75414d7dcfa2ae8951186221970  lua-openrc-0.2.tar.gz
d195d39ffdcb6334b885288ba501138b4c63db2387ea8e8c041919a858274647be0f71855eb9c3d4ffeeaf07b18862e7ff865fe11b23e16995935c17f547511f  0001-build-fix-for-openrc-0.14.patch"
